<?PHP

	include("../../includes/configAjax.php");
	include("../../includes/forum/board.class.php");
	include("../../includes/forum/topic.class.php");
	include("../../includes/forum/post.class.php");
	$board = new board();
	$topic = new topic();
	$post = new post();
	$error=false;
	
	$message = $_GET['message'];
	$title = $_GET['title'];
	
	if($_SESSION['logged_in']!=true){
		echo "You must be logged in to post";
		//$html = file_get_contents('../../styles/'.$activeStyle.'/t/login.html');
		//echo $html;
		die();
	}
	
	if(strlen($message)<3){
		$error = true;
		echo "Your message must be at least 3 characters long";
	}
	if(strlen($title)<3){
		if($error){
			echo "<br>";	
		}
		$error = true;
		echo "Your title must be at least 3 characters long";
	}
	
	switch($_GET['type']){
		
		case "reply":
			$topic = $db->prep($_GET['id']);
			if(is_numeric($_GET['id'])){
				$lockCheck = $db->query("SELECT locked FROM ".$prefix."forum_topic WHERE topic_id=".$topic);
				while($row = mysql_fetch_object($lockCheck)){
					if($row->locked==0 || ($row->locked==1 && $users->auth($db,2))){
						if(!$error){
							$out = $post->postReply($db, $message, $title, $topic);
							if(!$out){
								echo "Insufficient permission to reply";	
							}
						}
					} else {
						echo "Forum is locked";	
					}
				}	
			} else {
				echo "Invalid topic number!";	
			}
		break;
		case "quote":
			$topic = $db->prep($_GET['id']);
			$quote = $db->prep($_GET['quote']);
			if(is_numeric($_GET['id'])){
				$lockCheck = $db->query("SELECT locked FROM ".$prefix."forum_topic WHERE topic_id=".$topic);
				while($row = mysql_fetch_object($lockCheck)){
					if($row->locked==0 || ($row->locked==1 && $users->auth($db,2))){
						if(!$error){
							$out = $post->postReply($db, $message, $title, $topic, $quote);
							if(!$out){
								echo "Insufficient permission to reply";	
							}
						}
					} else {
						echo "Forum is locked";	
					}
				}	
			} else {
				echo "Invalid topic number!";	
			}
		break;
		case "newtopic":
			$boardId = $db->prep($_GET['id']);
			$categoryId = $db->prep($_GET['categoryId']);
			if(is_numeric($_GET['id']) && is_numeric($_GET['categoryId'])){
				if($users->auth($db, 2)){
					$sticky = $db->prep($_GET['sticky']);
				} else {
					$sticky = "no";
				}
				if(!$error){
					$out = $topic->postNewTopic($db, $post, $message, $title, $boardId, $categoryId, $sticky);
					if(!$out){
						echo "Insufficient permission to start a new topic";	
					} else {
						echo "newtopic";
					}
				}
			} else {
				echo "Invalid category or user";
			}
		break;
		case "edit":
			$postId = $db->prep($_GET['id']);
			$userId = $db->prep($_GET['uid']);
			if(is_numeric($_GET['id']) && is_numeric($_GET['uid'])){
				if( ($userId == $_SESSION['uid']) || ($users->auth($db, 2)) ){// 
					if(!$error){
						$post->editPost($db, $message, $title, $postId);
					}
				} else {
					echo "You are not the original poster.";	
				}
			} else {
				echo "invalid post or user";
			}
		break;
	
	}
	
?>